const inputIdMap = {
  nickname: 1,
  mobile: 2,
  email: 3,
}

const UserHttp = getApp().UserHttp()
const CommonHttp = getApp().CommonHttp()
const maxSecond = 60;
let currentSecond = maxSecond;
const INPUTTypeMap = {
  typeName: {
    placeholder: '请输入昵称',
    _id: inputIdMap.nickname,
    title: '修改昵称',
    name: '昵称'

  },
  typeMobile: {
    placeholder: '请输入11位手机号',
    _id: inputIdMap.mobile,
    title: '修改手机号',
    name: '手机号'
  },
  typeEmail: {
    placeholder: '请输入邮箱',
    _id: inputIdMap.email,
    title: '修改邮箱',
    name: '邮箱'
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    model: {},
    hiddenmodalput: true,
    inputType: {},
    inputContent: null,
    checkCodeStatus: {
      title: '获取验证码',
      enable: false
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const event = this.getOpenerEventChannel()
    event && event.on('dataSource', model => {
      const param = {
        cashierId: model.cashierId
      };
      this.reqParam = param
      this.setData({
        model
      })
    })
  },
  nameTrigger: function () {
    this.setData({
      hiddenmodalput: false,
      inputType: INPUTTypeMap.typeName,
      inputContent: this.data.model.nickname
    })
  },
  mobileTrigger: function () {
    this.setData({
      hiddenmodalput: false,
      inputType: INPUTTypeMap.typeMobile,
      inputContent: this.data.model.mobile
    })
  },
  emailTrigger: function () {
    this.setData({
      hiddenmodalput: false,
      inputType: INPUTTypeMap.typeEmail,
      inputContent: this.data.model.email
    })
  },
  avatarTrigger: function () {
    const event = this.getOpenerEventChannel();
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      camera: 'back',
      success: async res => {
        console.log(res);
        const arrs = res.tempFiles;
        if (arrs.length > 0) {
          let data = await CommonHttp.asyncUploadImage({
            file: arrs[0].tempFilePath,
            pathName: 'weChatMiniProgram'
          })
          debugger
          if (data.status == 200) {
            const {
              result
            } = data
            const model = {
              ...this.data.model,
              avatar: result.url
            }
            const editRes = UserHttp.actionEditAvatar(model);
            debugger
            if (editRes.status == 200) {
              event.emit('callback', model)
              this.setData({
                model
              })
            }
          }
        }

      }
    })
  },
  getCheckCode: async function (e) {
    const {
      checkCodeStatus
    } = this.data;
    if (checkCodeStatus.enable == false) {
      return
    }
    const param = {
      mobile: this.data.inputContent
    }
    const res = await CommonHttp.getSMSCheckcode(param)
    if (res.status == 200) {
      this.timer && clearInterval(this.timer);
      this.timer = setInterval(() => {
        if (currentSecond == 1) {
          this.setData({
            checkCodeStatus: {
              enable: false,
              title: '获取验证码'
            }
          })
          currentSecond = maxSecond;
          this.timer && clearInterval(this.timer)
        } else {
          this.setData({
              checkCodeStatus: {
                enable: false,
                title: `(${currentSecond})s`
              }
            })
            --currentSecond
        }
      }, 1000);
    }
  },
  inputHandler: function (e) {
    this.setData({
      inputContent: e.detail.value,
      checkCodeStatus: {
        enable: e.detail.value && e.detail.value.length == 11,
        title: '获取验证码'
      }
    })
  },
  checkCodeHandler: function (e) {
    this.checkCode = e.detail.value
  },
  cancel: function (e) {
    this.setData({
      hiddenmodalput: true,
      inputType: {},
      inputContent: this.data.model.code
    })
  },
  confirm: async function (e) {
    let {
      model
    } = this.data
    let res = {};
    if (this.data.inputType._id == inputIdMap.nickname) {
      if (this.data.inputContent == null || this.data.inputContent.length == 0) {
        return;
      }
      model = {
        ...model,
        nickname: this.data.inputContent
      }
      res = await UserHttp.actionEditNickName(model)
    } else if (this.data.inputType._id == inputIdMap.mobile) {
      const mobileOK = (this.data.inputContent != null || this.data.inputContent.length == 11);
      const checkCodeOk = this.checkCode && this.checkCode.length > 2;
      if (mobileOK && checkCodeOk) {
        model = {
          ...model,
          mobile: this.data.inputContent,
          captcha:this.checkCode
        }
        res =  await UserHttp.actionEditMobile(model)

      } else {
        return
      }


    } else if (this.data.inputType._id == inputIdMap.email) {
      if (this.data.inputContent == null || this.data.inputContent.length == 11) {
        return;
      }
      model = {
        ...model,
        email: this.data.inputContent
      }
      res = await UserHttp.actionEditEmail(model)
    }
    if (res.status == 200) {
      this.setData({
        model,
        hiddenmodalput: true
      })
      this.getOpenerEventChannel().emit('callback', model)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  popMethod: function () {
    getApp().popMethod()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})